package com.zl.tby.service.impl;

import com.zl.tby.model.Demo;
import com.zl.tby.repository.DemoRepo;
import com.zl.tby.service.DemoService;
import org.noear.solon.annotation.Component;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

/**
 * @author wangjl
 * @title: ApiService
 * @projectName share
 * @description: 演示实现类
 * @date 2024/5/916:08
 */
@Component
public class DemoServiceImpl implements DemoService {

    @Db
    private DemoRepo demoRepo;

    /**
     * @author wangjl
     * @description: 创建demo
     */
    @Override
    public Demo create(Demo demo) {
        demoRepo.insert(demo, true);
        return demo;
    }


    /**
     * @author wangjl
     * @description: 获取demo
     */
    @Override
    public Demo get(Long id) {
        return demoRepo.selectById(id);
    }

    /**
     * @author wangjl
     * @description: 删除
     */
    @Override
    public int delete(Long id) {
        return demoRepo.deleteById(id);
    }

    /**
     * @author wangjl
     * @description: 更新demo
     */
    @Override
    public int update(Demo demo) {
        return demoRepo.updateById(demo, false);
    }

    @Override
    public IPage<Demo> page(int start, int size, String title) {

        return demoRepo.selectPage(start, size, mp -> {
            mp
                    .whereTrue()
                    .andIf(StringUtils.isNotEmpty(title), "title like ?", "%" + title + "%");
        });
    }


}
